package com.laikan.reader.core.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.core.bean.CommentBean;
import com.laikan.reader.core.utils.PublicUtil;
import com.laikan.reader.core.view.MyTextView;

import java.util.ArrayList;

@SuppressWarnings ("rawtypes")
public class CommentAdapter extends LaikanBaseAdapter<CommentBean> {

    public Drawable C_999999_reply, C_999999_clicktosupport, C_66ccdd_clicktosupport;
    private ItemClickListener itemClickListener;

    public CommentAdapter(Context context,ArrayList<CommentBean> datas) {
        super(context, datas);
        // TODO Auto-generated constructor stub
        C_66ccdd_clicktosupport = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_click_to_support), Color.parseColor("#66ccdd")));
        C_999999_clicktosupport = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_click_to_support), Color.parseColor("#999999")));
        C_999999_reply = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_reply), Color.parseColor("#999999")));
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.lk_comment_item_newcopy, null);
            holder.person_img = (ImageView)convertView.findViewById(R.id.head_reviewer_img);
            holder.username = (TextView)convertView.findViewById(R.id.head_reviewer_name);
            holder.comment_ding = (ImageView)convertView.findViewById(R.id.comment_ding);
            //            holder.comment_jing = (TextView) convertView.findViewById(R.id.comment_jing);
            //            holder.comment_jing = (ImageView) convertView.findViewById(R.id.comment_jing);
            holder.comment_content = (TextView)convertView.findViewById(R.id.head_comment_content);
            holder.comment_title = (MyTextView)convertView.findViewById(R.id.comment_title);
            holder.comment_time = (TextView)convertView.findViewById(R.id.head_comment_time);
            holder.iv_click_to_support = (ImageView)convertView.findViewById(R.id.iv_clicktosupport);
            holder.iv_reply = (ImageView)convertView.findViewById(R.id.iv_reply);
            holder.tv_click_to_support = (TextView)convertView.findViewById(R.id.tv_clicktosupport);
            holder.tv_reply = (TextView)convertView.findViewById(R.id.tv_reply);
            holder.lr_item_root = (RelativeLayout)convertView.findViewById(R.id.lr_item_content);
            holder.rl_clicktosupport = (RelativeLayout)convertView.findViewById(R.id.rl_clicktosupport);
            holder.rl_clickreply = (RelativeLayout)convertView.findViewById(R.id.rl_clickreply);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        if (datas.get(position).isFavor()) {
            holder.iv_click_to_support.setImageDrawable(C_66ccdd_clicktosupport);
        }else {
            holder.iv_click_to_support.setImageDrawable(C_999999_clicktosupport);
        }
        holder.iv_reply.setImageDrawable(C_999999_reply);
        if(!TextUtils.isEmpty(datas.get(position).getReplyCount()) && datas.get(position).getReplyCount().length() > 2){
            holder.tv_reply.setText(R.string.tv_comment_more);
        }else {
            holder.tv_reply.setText(datas.get(position).getReplyCount());
        }

        if(!TextUtils.isEmpty(datas.get(position).getLikeCount()) && datas.get(position).getLikeCount().length() > 2){
            holder.tv_click_to_support.setText(R.string.tv_comment_more);
        }else {
            holder.tv_click_to_support.setText(datas.get(position).getLikeCount());
        }
        holder.rl_clicktosupport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onClickToSupport(view, position);
            }
        });

        holder.lr_item_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onReplyClick(view, position);
            }
        });
        holder.rl_clickreply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onReplyClick(view, position);
            }
        });
        holder.username.setText(datas.get(position).getUserName());
//        holder.comment_time.setText(PublicUtil.getDateToString(Long.parseLong(datas.get(position).getPublishTime())));
        holder.comment_time.setText(datas.get(position).getReviewShowTime());
        holder.person_img.setImageURI(Uri.parse(datas.get(position).getUserIcon()));
        holder.comment_content.setText(datas.get(position).getSummary());
        //        KLog.d("TAG", "comment_jing is  "  +datas.get(position).isBest()+"------------------------------");
        if(TextUtils.isEmpty(datas.get(position).getTitle())){
            holder.comment_title.setVisibility(View.GONE);
        }else {
            holder.comment_title.setVisibility(View.VISIBLE);
            if (datas.get(position).isBest()) {
                String title = "[精]" + datas.get(position).getTitle();
                holder.comment_title.setSpecifiedTextsColor(title, "[精]", Color.parseColor("#66ccdd"));
            }else {
                holder.comment_title.setText(datas.get(position).getTitle());
            }
        }
        //		if (datas.get(position).isTop()) {
        //			holder.comment_ding.setVisibility(View.VISIBLE);
        //		}else{
        //			holder.comment_ding.setVisibility(View.GONE);
        //		}
        return convertView;
    }

    private class ViewHolder {

        private ImageView person_img, comment_ding, iv_click_to_support, iv_reply;;
        private TextView comment_time, comment_content, username, comment_jing, tv_click_to_support, tv_reply;
        private MyTextView comment_title;
        private RelativeLayout lr_item_root, rl_clicktosupport, rl_clickreply;
    }

    public void setItemClickListener(ItemClickListener listener){
        this.itemClickListener = listener;
    }
    public interface ItemClickListener{
        void onItemClick(View view, int position);
        void onReplyClick(View view, int position);
        void onClickToSupport(View view, int position);
    }
}
